package com.asom.player;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Toast;

import com.asom.player.service.MPlayerService;
import com.asom.player.service.Player;
import com.markupartist.android.widget.ActionBar.Action;
import com.markupartist.android.widget.ActionBar.IntentAction;

public class DashboardActivity extends CustomActivity {

	boolean isBounded;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.dashboard_layout);

		initializeActionBar(getString(R.string.app_name));
	}

	@Override
	protected void onStart() {
		super.onStart();

		Intent intent = new Intent(this, MPlayerService.class);
		bindService(intent, connection, Context.BIND_AUTO_CREATE);
	}

	@Override
	protected void onStop() {
		super.onStop();

		if (isBounded) {
			unbindService(connection);
			isBounded = false;
		}
	}

	ServiceConnection connection = new ServiceConnection() {

		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			player = (Player) arg1;

			player.setContentResolver(getContentResolver());
			player.setContext(getApplicationContext());

			isBounded = true;
		}

		public void onServiceDisconnected(ComponentName arg0) {
			isBounded = false;
		}
	};

	@Override
	public void onConfigurationChanged(Configuration newConfig) {

		this.setContentView(R.layout.dashboard_layout);
		this.initializeActionBar(getString(R.string.app_name));
		// AdsHolder.putAdsHere(this, R.id.adsBottom);

		super.onConfigurationChanged(newConfig);
	}

	private void initializeActionBar(String title) {

		final Action shareAction = new IntentAction(this, null,
				R.drawable.ic_title_share_default);

		if (actionBar != null) {
			actionBar.setTitle(title);
			actionBar.addAction(shareAction);
		}
	}

	public void onActionOneClick(View v) {
		Toast.makeText(this, R.string.dashboard_action_one, Toast.LENGTH_SHORT)
				.show();

		Intent dashboardToList = new Intent(DashboardActivity.this,
				ListActivity.class);
		dashboardToList.putExtra("contentType",
				getString(R.string.dashboard_action_one));
		startActivity(dashboardToList);
	}

	public void onActionTwoClick(View v) {
		Toast.makeText(this, R.string.dashboard_action_two, Toast.LENGTH_SHORT)
				.show();

		Intent dashboardToList = new Intent(DashboardActivity.this,
				ListActivity.class);
		dashboardToList.putExtra("contentType",
				getString(R.string.dashboard_action_two));
		startActivity(dashboardToList);
	}

	public void onActionThreeClick(View v) {
		Toast.makeText(this, R.string.dashboard_action_three,
				Toast.LENGTH_SHORT).show();

		Intent dashboardToList = new Intent(DashboardActivity.this,
				ListActivity.class);
		dashboardToList.putExtra("contentType",
				getString(R.string.dashboard_action_three));
		startActivity(dashboardToList);
	}

	public void onActionFourClick(View v) {
		Toast
				.makeText(this, R.string.dashboard_action_four,
						Toast.LENGTH_SHORT).show();

		Intent dashboardToList = new Intent(DashboardActivity.this,
				ListActivity.class);
		dashboardToList.putExtra("contentType",
				getString(R.string.dashboard_action_four));
		startActivity(dashboardToList);
	}

	public void onActionFiveClick(View v) {
		Toast
				.makeText(this, R.string.dashboard_action_five,
						Toast.LENGTH_SHORT).show();

		Intent dashboardToList = new Intent(DashboardActivity.this,
				ListActivity.class);
		dashboardToList.putExtra("contentType",
				getString(R.string.dashboard_action_five));
		startActivity(dashboardToList);
	}

	public void onActionSixClick(View v) {

//		Intent dashboardToSocial = new Intent(DashboardActivity.this,
//				Example.class);
//		startActivity(dashboardToSocial);
//		Toast.makeText(this, "Social Stuff", Toast.LENGTH_SHORT).show();
	}
}